package com.yb.util.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.yb.domain.member.TsSysuser;
import com.yb.domain.system.TsMenu;
import com.yb.util.comm.Contants;
import com.yb.util.file.MenuUtil;

public class MenuTag2 extends TagSupport {

	private static final long serialVersionUID = 1L;
	String basePath;

	@SuppressWarnings("unchecked")
	public int doEndTag() throws JspException {
		try {
			HttpSession session = pageContext.getSession();
			JspWriter out = pageContext.getOut();
			TsSysuser baseInfo = (TsSysuser) session
					.getAttribute(Contants.SESSION_USER);
			HttpServletRequest request = (HttpServletRequest) pageContext
					.getRequest();
			String path = request.getContextPath();
			basePath = request.getScheme() + "://" + request.getServerName()
					+ ":" + request.getServerPort() + path + "/";
			if (null == baseInfo) {
				out.print("<script>window.location.href='" + basePath
						+ "login.html'</script>");
				return TagSupport.SKIP_PAGE;
			}
			String level = null == baseInfo.getType() ? "0" : baseInfo
					.getType().toString();
			if("3".equals(level)){
				if(baseInfo.getIsBz() == 1)
					level = "8";
			}
			if("2".equals(level)){
				if(baseInfo.getIsBz() == 1)
					level = "9";
			}
			List<TsMenu> menuList = MenuUtil.getMenuList();
			StringBuffer strBuffer = new StringBuffer();
			strBuffer.append("<link rel='stylesheet' href='" + basePath
					+ "resource/css/management.css' type='text/css'></link>");
			strBuffer.append("<link rel='stylesheet' href='" + basePath
					+ "resource/css/easyui/default/easyui.css' type='text/css'></link>");
			strBuffer.append("<script type='text/javascript' src='" + basePath
					+ "resource/js/jquery-1.4.2.js' charset='utf-8'></script>");
			strBuffer.append("<script type='text/javascript' src='" + basePath
					+ "resource/js/security.js' charset='utf-8'></script>");
			strBuffer.append("<script type='text/javascript' src='" + basePath
					+ "resource/js/easyui/jquery.easyui.min.js' charset='utf-8'></script>");
			strBuffer.append("<script type='text/javascript' src='" + basePath
					+ "resource/js/easyui/easyui-lang-zh_CN.js' charset='utf-8'></script>");
			
			strBuffer.append("<script type='text/javascript' src='" + basePath
					+ "management.js' charset='utf-8'></script>");
			strBuffer.append("<div id='funcArea' class='funcArea'>");
			for (Iterator iterator = menuList.iterator(); iterator.hasNext();) {
				TsMenu menu = (TsMenu) iterator.next();
				menu.bulidMenuByUserLevel(level);
				String tilteId = menu.getDescr();
				if ((null != menu.getChildren() && menu.getChildren().size() > 0)
						|| ("anyone".equals(menu.getAllowGroups()))) {
					strBuffer.append("<h3>" + menu.getName() + "</h3>");
					bulidMenu(menu.getChildren(), strBuffer, level, tilteId);
				}
			}
			strBuffer.append("</div>");
			out.print(strBuffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	@SuppressWarnings("unchecked")
	private StringBuffer bulidMenu(List<TsMenu> menuList,
			StringBuffer strBuffer, String level, String tilteId) {
		for (Iterator iterator = menuList.iterator(); iterator.hasNext();) {
			TsMenu menu = (TsMenu) iterator.next();
			String needPassword = menu.getNeedPassword();
			String url = "hasChildren".equals(menu.getUrl()) ? "javascript:void(0)"
					: basePath + bulidUrl(menu.getUrl(), menu.getDescr());
			strBuffer.append("<div class='func' align='center' title='"+menu.getTip()+"' uri='"+url+"' needPassword='"+needPassword+"'>");
			strBuffer.append("<img width='48px' height='48px' src='"+menu.getIcon()+"'/><br/><span>"+menu.getName()+"</span></div>");
		}
		return strBuffer;
	}

	private String bulidUrl(String url, String cls) {
		if (!"hasChildren".equals(url))
			if (url.indexOf("?") == -1)
				url += "?menuCls=" + cls;
			else
				url += "&menuCls=" + cls;
		return url;
	}
}
